Home > Net >  The body I am send through as a POST request gives me a bad request, where am I going wrong using HT
The body I am send through as a POST request gives me a bad request, where am I going wrong using HT

Time:07-22

The body I am send through as a POST request gives me a bad request, where am I going wrong using HTTP.NET.

Here is my code, I've tried deserializing the json body and I have also tried reading it from a file but still get bad request, am I missing something

            string accessKeyId = "<KEY>";
            string secretAccessKey = "<KEY>";
            var clientSecurity = new ClientSecurity(new SignatureVersionFactory());
            var url = GetCreateSchedulingJobUrl(@"C:\Users\me\Desktop\test.txt");

            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url);
            //httpRequestMessage.Headers.Accept.Add("Content-Type", "application/json");
            //httpRequestMessage.Headers.Accept.Add(new 
            MediaTypeWithQualityHeaderValue("application/json"));
            using var client = new HttpClient();
             
            var body = File.ReadAllText(@"C:\Users\me\Desktop\test.txt");
            var content = JsonConvert.SerializeObject(body);
            httpRequestMessage.Content = new StringContent(content);
            httpRequestMessage.Content.Headers.ContentType = new 
            MediaTypeHeaderValue("application/json");
            clientSecurity.SignRequestAsync(httpRequestMessage, accessKeyId, 
            secretAccessKey, SigningAlgorithms.S3S);
            var response = client.SendAsync(httpRequestMessage).Result;
            int statusCode = (int)response.StatusCode;
            string statusMsg = response.Content.ReadAsStringAsync().Result;

I have also tried:

                var body = @"{
                                "   "\n"  
                             @"    ""name"": ""Some name""
                             "   "\n"  
                             @"}";
                var content = JsonConvert.SerializeObject(body);
                request.Content = new StringContent(content);
                request.Content.Headers.ContentType = new 
                MediaTypeHeaderValue("application/json");
                response = httpClient.PostAsync(webUrl,request.Content).Result;

BUT still get bad request, are there alternative ways of doing this?

CodePudding user response:

Do this and try again

var content = File.ReadAllText(@"C:\Users\me\Desktop\test.txt");
// var content = JsonConvert.SerializeObject(body);

If it works tidy up afterwards.

JsonSerialize() serializes objects to JSON strings. You are reading in a JSON string (an already serialized representation of an object) and then serializing it again. Have a look at this...

public class Obj
{
  public string name;
}
:
  var obj = new Obj { name = "value" };
  var json1 = JsonConvert.SerializeObject(obj);
  var json2 = "{ \"name\":\"value\" }";
  var json3 = JsonConvert.SerializeObject(json1);
  Console.WriteLine($"json1:{json1}");
  Console.WriteLine($"json2:{json2}");
  Console.WriteLine($"json3:{json3}");

which produces

json1:{"name":"value"}
json2:{ "name":"value" }
json3:"{\"name\":\"value\"}"

      <- plain old string ->
  • Related