Unable to upload image file via '/api/v2/uploads.json' through postman.
Hello
I just tried out to upload file, and also successfully uploaded and it's return token in response.
After successful uploaded using those token able to attach those file in comment of ticket but the attached file in to comment not open properly or we can say not upload properly.
My assumption is something i missed to provide while upload file using '/api/v2/uploads.json'
Please help me on that also find c# code for upload attachment.
Please have a look what i tried!
-
Hello Ghanshyam -- just to clarify, you were able to get it working using Postman? It's just your C# code that is having issues?
The primary sources of documentation are here. There are two steps involved:
2. Attaching file to a ticket comment
If you share your C# code, perhaps someone in the community can point out where the issue might be.
-
There are only one issue with my code, i'm able to sending .png file and get response back with Token but looks like image not properly uploaded, because when i'm download this uploaded image from zendesk ticket then there is no any content to show, please have a look my code,
using (var loClient = new HttpClient())
{
//convert image/file into binary
byte[] loBinaryFile = null;
using (var loBinaryReader = new BinaryReader(foUplodedFiles.InputStream))
{
loBinaryFile = loBinaryReader.ReadBytes(foUplodedFiles.ContentLength);
}loClient.BaseAddress = new Uri(msURL + lsRequestURL);
loClient.DefaultRequestHeaders.Accept.Clear();
loClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/binary"));
loClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", getAuthHeader(msUserName + "/token", msToken));MultipartFormDataContent loForm = new MultipartFormDataContent();
loForm.Headers.ContentType = new MediaTypeHeaderValue("application/binary");
loForm.Add(new ByteArrayContent(loBinaryFile, 0, loBinaryFile.Count()));var loRequestMessage = new HttpRequestMessage()
{
Method = HttpMethod.Post,
Content = loForm,
RequestUri = new Uri(msURL + lsRequestURL),
};HttpResponseMessage loAttachmentResponse = loClient.SendAsync(loRequestMessage).Result;
loAttachmentResponse.EnsureSuccessStatusCode();
if (loAttachmentResponse.IsSuccessStatusCode)
{
foResponseData = JObject.Parse(loAttachmentResponse.Content.ReadAsStringAsync().Result);
}
}protected string getAuthHeader(string userName, string password)
{
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", userName, password)));
return string.Format("Basic {0}", auth);
} -
I am having the same issue, I was able to upload the image or text file using the upload API however, the image and text file got distorted, I am using PHP to upload it below is my code.
<?php
$post_data = (object)[];
$post = (object)[];
$post->title = "Subject of the post";
$post->topic_id = 900000044523;
$post->notify_subscribers = false;
$post->details = "<p>Hello,</p><p> </p><p>I had lost all my data from Box.com..I have been using it from last few years and now I am lost it all..</p><p> </p><p>Can any one please help me?</p><p>Ashish Shah</p><p> </p><p>Login:***email address removed for privacy***</p>";
$subdomain = "****";
$email_address = "*****";
$password = "****";
$ch = curl_init();
$json = '{"post": {"title": "Another Post with code", "topic_id": 900000044523}, "notify_subscribers": false}';
$image = "test.txt"; // be careful that the path is correct
$data = fopen($image, 'rb');
$size = filesize($image);
$contents = fread($data, $size);
fclose($data);
$encoded = base64_encode($contents);
echo $encoded;
curl_setopt($ch, CURLOPT_URL, 'https://****.zendesk.com/api/v2/uploads.json?filename=test.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_USERPWD, $email_address . ':' . $password);
$headers = array();
$headers[] = 'Content-Type: application/binary';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r($result);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);I have written Hi in my text file, however, this become something else after uploading. M I missing something? could someone please correct it.
-
Hi Tariq Husain! I'll respond to your other post in the community, since this one was specifically about tickets and you are looking for options in community comments.
I'll drop you a line shortly!
-
Looking for some assisting identifying what I am doing wrong? Thank you!
Headers:
Params + Call:
-
Hi Cesar Perez,
Use "--data-binary" only when you're using the command line tool cURL.
For Postman, it should look something like this:
On the "Body" tab, you would click "Select File" and then browse for the file that you want to upload. Give that a try and see how it goes!
-
Thanks! That did it. I assume the upload token is good for 1 month, used as many times as possible?
-
Actually Cesar, tokens are only good for a few days and once used are invalidated. See Upload Files
... The token is valid for 3 days
Note: ...Once an attachment is associated with a ticket or post, visibility is restricted to users with access to the ticket or post with the attachment. -
Awesome, thanks!
You don't happen to have a snippet of python code? :)
Thanks, anyways. It was really helpful.
-
Check out the Python wrapper libraries located here: https://developer.zendesk.com/rest_api/docs/api-clients/python
These are not officially supported by Zendesk, but can offer an easier, language-specific entry point into Zendesk Support functionality.
Also, since you're in Postman, you can get your API request translated to a number of different languages and tools directly. Look for the "Code" button in the upper right and choose Python from the drop-down to see an example:
-
Thank you, this is very helpful!
Please sign in to leave a comment.
11 Comments