Maya Stern
Maya Stern

Reputation: 73

Error response when trying to send JSON body using HttpRequestMessage

I need to send body parameters using JSON. I'm working with Printify APIs. I keep getting error response from server, that my body parameters are empty.

This is the command I'm trying to create:

POST https://api.printify.com/v1/uploads/images.json

it needs to includes the body parameters (that's an example from the API guide):

BODY PARAMETER (UPLOAD IMAGE BY BASE64-ENCODED CONTENTS)
{
    "file_name": "image.png",
    "contents": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
}

This is my code:

public class ImageContentsParams
{
    public string FileName { get; set; }
    public string Contents { get; set; }
}

ImageContentsParams imageContents = new ImageContentsParams();
imageContents.FileName = "example.png";
byte[] imageArray = System.IO.File.ReadAllBytes("D:\\example.png");
imageContents.Contents = Convert.ToBase64String(imageArray);
var json = JsonConvert.SerializeObject(imageContents);

using (HttpClient client = new HttpClient())
{
    var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.printify.com/v1/uploads/images.json");

    request.Content = jsonBody;

    using (HttpResponseMessage res = await client.SendAsync(request))
    {
        // here I'm checking the returned response
    }
}

I keep getting the following error response from Prinify server (this is after parsing):

Code: 10100 Message: Validation failed. Reason: file_name: The file name field is required. contents: The contents field is required when url is not present. url: The url field is required when contents is not present.

What am I doing wrong???

Upvotes: 1

Views: 644

Answers (2)

Diego Bascans
Diego Bascans

Reputation: 1149

Probably your FileName in JsonContentParams must be renamed to be the same as the JSON that is expected in that service. This is because the params will be serialized and will not match with the specification of the service.

public class ImageContentsParams
{
    public string file_name { get; set; }
    public string contents { get; set; }
}

Another solution can be to add the JsonProperty attribute, in order to not to refactor all your code.

public class ImageContentsParams
{
    [JsonProperty("file_name")] 
    public string FileName { get; set; }

    [JsonProperty("contents")] 
    public string Contents { get; set; }
}

Try something like that and tell me if it works.

Upvotes: 0

DNakevski
DNakevski

Reputation: 310

FileName property will be serialized into fileName and that will not match file_name convention. Change the property to match or specify mapping schema on the serialization to translate FileName into file_name. You can do the mapping by specifyng [JsonProperty("file_name")] attribute on the FileName property. Something like this:

public class ImageContentsParams
{
    [JsonProperty("file_name")] 
    public string FileName { get; set; }
    public string Contents { get; set; }
}

Upvotes: 1

Related Questions