XF 2.2 REST API read and get attachment

TheSkullKid

Active member
I want to get the attachment from a post, but it looks like that this is not working:

To get the attachment key
Code:
curl --header 'XF-Api-Key: KEYXYZ'
    --request POST http://localhost/api/attachments/new-key/ \
    --data type=post \
    --data context[post_id]=1234
Now try to get the attachment
Code:
curl --header 'XF-Api-Key: KEYXYZ' \
    --request GET http://localhost/api/attachments \
    --data key=GENERATED_KEY_FROM_ABOVE_COMMAND
This is the result:
Code:
HTTP/1.1 400 Bad Request
Date: Fri, 23 Sep 2022 10:10:01 GMT
Server: Apache/2.4.52 (Win64) OpenSSL/1.1.1m mod_fcgid/2.3.10-dev
X-Powered-By: PHP/7.4.27
XF-Latest-Api-Version: 1
XF-Used-Api-Version: 1
XF-Request-User: 1
XF-Request-User-Extras: {"conversations_unread":0,"alerts_unviewed":0}
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: private, no-cache, max-age=0
X-XF-Debug-Stats: {"time":2.7067,"queries":8,"memory":14.15}
Content-Encoding: gzip
Vary: Accept-Encoding
Content-Length: 137
Connection: close
Content-Type: application/json; charset=utf-8

{
  "errors": [
    {
      "code": "required_input_missing",
      "message": "Required input missing: key",
      "params": {
        "missing": [
          "key"
        ]
      }
    }
  ]
}
Someone any ideas?
Thanks
 

TheSkullKid

Active member
I changed the code to
Code:
curl --header 'XF-Api-Key: KEYXYZ' \
    --request GET http://localhost/xf226/api/attachments?key=GENERATED_KEY_FROM_ABOVE_COMMAND
But still do not receive the attachments:
Code:
HTTP/1.1 200 OK
Date: Wed, 28 Sep 2022 09:05:20 GMT
Server: Apache/2.4.52 (Win64) OpenSSL/1.1.1m mod_fcgid/2.3.10-dev
X-Powered-By: PHP/7.4.27
XF-Latest-Api-Version: 1
XF-Used-Api-Version: 1
XF-Request-User: 1
XF-Request-User-Extras: {"conversations_unread":0,"alerts_unviewed":0}
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: private, no-cache, max-age=0
X-XF-Debug-Stats: {"time":2.7263,"queries":9,"memory":14.45}
Content-Encoding: gzip
Vary: Accept-Encoding
Content-Length: 43
Connection: close
Content-Type: application/json; charset=utf-8

{
  "attachments": []
}

How to upload attachments?
Code:
HTTP/1.1 400 Bad Request
Date: Wed, 28 Sep 2022 09:09:28 GMT
Server: Apache/2.4.52 (Win64) OpenSSL/1.1.1m mod_fcgid/2.3.10-dev
X-Powered-By: PHP/7.4.27
XF-Latest-Api-Version: 1
XF-Used-Api-Version: 1
XF-Request-User: 1
XF-Request-User-Extras: {"conversations_unread":0,"alerts_unviewed":0}
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: private, no-cache, max-age=0
X-XF-Debug-Stats: {"time":2.6887,"queries":9,"memory":14.21}
Content-Encoding: gzip
Vary: Accept-Encoding
Content-Length: 142
Connection: close
Content-Type: application/json; charset=utf-8

{
  "errors": [
    {
      "code": "required_input_missing",
      "message": "Required input missing: attachment",
      "params": {
        "missing": [
          "attachment"
        ]
      }
    }
  ]
}
 

Kirby

Well-known member
I am not entirely sure what you are trying to do, but let's assume you want to get information about attachments in post ID 1234

Code:
curl --header 'XF-Api-Key: KEYXYZ' http://localhost/api/posts/1234/

This should return a Post result with member Attachments which is an array of Attachment

If you want to get the attachment data (eg. image binary, etc.) you have to make an API call using the attachment_id from Attachment (let's assume that the ID is 5678):

Code:
curl --header 'XF-Api-Key: KEYXYZ' http://localhost/api/attachments/5678/data/
 

TheSkullKid

Active member
I am not entirely sure what you are trying to do, but let's assume you want to get information about attachments in post ID 1234

Code:
curl --header 'XF-Api-Key: KEYXYZ' http://localhost/api/posts/1234/

This should return a Post result with member Attachments which is an array of Attachment

If you want to get the attachment data (eg. image binary, etc.) you have to make an API call using the attachment_id from Attachment (let's assume that the ID is 5678):

Code:
curl --header 'XF-Api-Key: KEYXYZ' http://localhost/api/attachments/5678/data/
Thanks for that, but how to upload attachments via API?
 

Kirby

Well-known member
Assuming you want to add a new attachment from file test.txt in current directory to an existing post with ID 1234 using a user key and you do not yet have an attachment key yet:

Generate a new key:
Code:
curl --header 'XF-Api-Key: KEYXYZ' -F type=post -F context[post_id]=1234 -F attachment=@./test.txt http://localhost/api/attachments/new-key/

Upon success, this should return a JSON with member key.
If you need to upload further attachments: https://xenforo.com/community/pages/api-endpoints/#route_post_attachments_

Afterwards associate the attachment(s) from the key to the post by editing it:
Code:
curl --header 'XF-Api-Key: KEYXYZ' --request POST --data silent=1 --data attachment_key=<key> http://localhost/api/posts/1234/

If you want to upload attachments to a new post in an existing thread, the context would be thread_id=X, for a new thread it would be node_id=X.
 
Last edited:
Top