client_id is used for asynchronous uploads, we notify that client via the message bus that an upload was processed once it is process, which is why it should not matter if synchronous is enabled. There is no messaging.
This may just be a really weird PHP API, but where do you shuffle the actual file data to the server? I don’t know your _curl(), but it looks like it just gets a URL and some query parameters.
Curl is a tool and libcurl is a library for transferring data with URL syntax. It not just gets a URL and some query parameters
Before, I can use this curl function to submit data to discourse server when I create a new topic.
Look down a successful example to create a new topic using the api.
Oh, I know what curl is. I’m just pointing out that I don’t see where you’re sending the actual contents of the file you are supposedly uploading to Discourse. Without contents I don’t expect Discourse to be happy with the upload, so I wouldn’t expect a successful result.
Get rid of the function http_build_query, we can upload image successfully!
public function upload_file($user) {
$url = 'http://hostname/uploads?api_key=api_key&api_username=api_username';
$poststr = http_build_query(array('username' => $user, 'type' => 'image', 'file' => '/uploads/file/filename', 'synchronous' => 'true')); // get rid of the function http_build_query, we can upload image successfully!
$data = _curl($url, $poststr)); // _curl() : a custom function
return $data;
}
After change
$poststr = array('username' => $user, 'type' => 'image', 'file' => '/uploads/file/filename', 'synchronous' => 'true'); // get rid of the function http_build_query, we can upload image successfully!
In summary, when we submit data to discourse server using curl, we may use three method to post parameters. You should try more methods when you encounter problems.
i am trying to upload image using ajax, and getting 400 bad request.
but there is no message so i can see what is going on.
Have my csrf token, and also logged in but still nothing.
I’m not really sure what all you will need to do to get it to work via ajax (api key, csrf, cors, etc?), but here is how you can get an upload to work via the api (this is for an avatar upload, but it is the same endpoint, you probably just need to change the type):
i actually want to first upload image, and than with that image to make a post.
i made posts without api_key and api_username since i have csrf token and cookie,
so i guess i don’t need for this upload too.
so as you can see, without api_key and api_username.
but can you tell me more about this type, what should i put if i want to upload image before post?
and user_id, i don’t have user id, and i didn’t use it for any of the for any calls until now.
so, i made a call, uploaded picture, but there is no url to the picture in the response, only success : OK.
is there a way to get url to the image in the response?
One of the issues you are having has to do with the type parameter being set to ‘POST’ in all caps. It needs to be a value that complies with this rule:
Besides being used for determining if you are uploading an “avatar”, I’m not totally sure yet what all type is used for. So I would just set it to some all lowercase string like “upload”.
Here is an example file upload request and response API call: