Trouble Sending Image via HTTP Request

Pierre Paul Jacques

Активный пользователь
Регистрация
08.10.2023
Сообщения
134
Благодарностей
35
Баллы
28
Hello ZennoPoster Community and HTTP Request expert!

I'm trying to upload an image to an API that requires multipart/form-data, similar to how I'd do in Postman, where it works fine. However, I'm facing difficulties getting ZennoPoster to recognize the file path and send the file. Below is the information I have:

  • URL: https://api.medium.com/v1/images
  • Method: POST
  • Headers:
    • Authorization: Bearer {API_Token}
    • Content-Type should be multipart/form-data (but I'm not sure how ZennoPoster handles this)
  • File Path: C:\Zeno\01 - Project\image_test.jpg
  • Variables: I'm using a variable for the API token, which works well in Postman.
I've tried configuring the action properties in ZennoPoster, but the file is not found in the specified path. The Postman export shows a successful request with formdata, but translating this into ZennoPoster's settings is proving challenging. Here’s the format I’m attempting to replicate from Postman:

plaintextCopy code
--BoundaryString
Content-Disposition: form-data; name="image"; filename="image_test.jpg"
Content-Type: image/jpeg

[Binary image data]
--BoundaryString--

Do I need to use the "File upload to server" block before making the POST request? I am new to development and appreciate simple and clear instructions.
I guess i am close but for sure i miss something!

Thank you in advance for your Expertise!
PS the doc :

120035

120036
 

Pierre Paul Jacques

Активный пользователь
Регистрация
08.10.2023
Сообщения
134
Благодарностей
35
Баллы
28
Thank @dzair for his precious and quality help :

With this setting it works!

C#:
--FormBoundaryXYZ
Content-Disposition: form-data; name="image"; filename="image_test.jpg"
Content-Type: image/jpeg

{-Project.Directory-}image_test.jpg
--FormBoundaryXYZ--

120044
 
  • Спасибо
Реакции: dzair

Pierre Paul Jacques

Активный пользователь
Регистрация
08.10.2023
Сообщения
134
Благодарностей
35
Баллы
28
Hi everyone,

I've made progress with my project where I'm trying to upload images to Medium using ZennoPoster. Thanks to some tips, I'm able to get a response using the POST block. However, I'm stuck on a seemingly small but crucial detail. When I try to convert the POST block into C# code, I face some confusion in the documentation.

The error I’m getting from the API is:

{"errors":[{"message":"missing content-type header","code":-1}]}


C#:
// Define paths and tokens directly
string url = "https://api.medium.com/v1/images";
string filePath = @"C:\Zeno\01 - Project\image_test.jpg";
string apiToken = project.Variables["API_Token_Medium"].Value;

project.SendToLog("Starting the image upload process...", ZLog.Info);

// Check for the existence of the file
if (!File.Exists(filePath))
{
    project.SendToLog("File not found: " + filePath, ZLog.Error);
    return null;
}

project.SendToLog("File found: " + filePath, ZLog.Info);

// Read the file content
byte[] fileContent = File.ReadAllBytes(filePath);
if (fileContent.Length == 0)
{
    project.SendToLog("File content is empty.", ZLog.Error);
    return null;
}

// Create multipart/form-data content
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
string contentType = "multipart/form-data; boundary=" + boundary;
var postData = new List<byte>();
postData.AddRange(Encoding.UTF8.GetBytes("--" + boundary + "\r\n"));
postData.AddRange(Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"image\"; filename=\"" + Path.GetFileName(filePath) + "\"\r\n"));
postData.AddRange(Encoding.UTF8.GetBytes("Content-Type: application/octet-stream\r\n\r\n"));
postData.AddRange(fileContent);
postData.AddRange(Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"));

// Headers
string[] headers = {
    "Authorization: Bearer " + apiToken,
    "Content-Type: " + contentType
};

// Convert postData into a string for the HttpPost method
string postDataString = Encoding.UTF8.GetString(postData.ToArray());

// Execute the HTTP POST request
string response = ZennoPoster.HttpPost(
    url,
    postDataString,
    "",
    "",
    "utf-8",
    ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly,
    30000,
    "",
    "",
    true,
    0,
    headers,
    "",
    true);

// Logging the response or lack thereof
if (string.IsNullOrEmpty(response))
{
    project.SendToLog("The response from the Medium API is empty.", ZLog.Error);
}
else
{
    project.SendToLog("Received response from the Medium API: " + response, ZLog.Info);
    project.Variables["API_Response"].Value = response;
}

return null; // Make sure to return null if this is required by your ZennoPoster setup
I’ve got a response with the code, but there’s an error from the API saying the content-type header is missing. If someone could point me in the right direction to adjust this last setting, I’d be very grateful.

I’ve created aliases for log writing, hence the different appearance. I am seeking guidance to replicate this success in my C# script. Any help to clear up this final hurdle would be fantastic!

Would someone be able to guide me through the correct configuration for the Content-Type header in a multipart/form-data request, or point me to what I might be missing in my setup?





Thank you in advance Zenno Expert friends;-)
 

myndeswx

Client
Регистрация
15.05.2017
Сообщения
436
Благодарностей
104
Баллы
43
It could be because you're missing the user agent, in all http requests user agent must be the first header
give it a try like this

string[] headers = {
"User-Agent: Curl",
"Authorization: Bearer " + apiToken,
"Content-Type: " + contentType
};


The post method itself has the user-agent parameter, it goes here-
ZennoPoster.HttpPost(
url,
postDataString,
"",
"",
"utf-8",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.BodyOnly,
30000,
"",
"Curl",
true,
0,
headers,
"",
true);
 
  • Спасибо
Реакции: Pierre Paul Jacques

Кто просматривает тему: (Всего: 0, Пользователи: 0, Гости: 0)