0

I have a problem getting a a request message I am getting the data that I need with a content type x-www-form-urlencoded but in a form of JSON(see image). I use Ngrok for simulating the passing of data to a server.
See image here for the form that I am getting image

The Problem is I always get a NULL value.

I already build a class that should've getting the data(I edited this one, before I use for the Entry part):

public class FormData
{
    public string @object { get; set; }
    public Entry[] entry { get; set; }
    public string resource_url { get; set; }
}

public class Entry
{
    public string uuid { get; set; }
    public string[] changed_fields { get; set; }
    public string time { get; set; }
}

My [HttpPost] in my Controller that I already used are:

  • using the formData class and returning it, adding ToString()
  • using JObject even though it is a Form (Trying my luck to get that JSON format)
  • using [FromBody],[FromQuery] (but I get a unsupported media type) I used FromForm but still getting a NULL value
  • Tried desrializing it
  • Tried using "object" to support all classes. Still Null.
  • Using public HttpResponseMessage
  • Tried using ApiController extension.

Still getting NULL Values on these.

Now I'm using this just to return a 200 response

public string Post(FormData deviceData)
{
    string please = deviceData.ToString();
    return please;
}

If you cant see the picture above, here is the request that I am getting:

POST /api/values HTTP/1.1
Host: localhost:50754
User-Agent: ServiceM8-Platform/1.0
Content-Length: 233
Accept: */*
Content-Type: application/x-www-form-urlencoded
X-Forwarded-For: 35.176.170.33
X-Original-Host: 1fbbf508.ngrok.io

{"object":"JOB","entry":[{"uuid":"d4798ca6-5022-4527-b4ac-b6c7e822490b","changed_fields":["status"],"time":"2018-09-28 09:43:32"}],"resource_url":"https:\/\/api.servicem8.com\/api_1.0\/JOB\/d4798ca6-5022-4527-b4ac-b6c7e822490b.json"}

Any help are welcome.

  • The reason it is not working is that the content-type header and the actual content type don't match. Can you post the code you are using to make the request? – ste-fu Sep 28 '18 at 10:08
  • Yep. That is why, but whyyyyy. Anyway here's the request that I am getting. what I need is to get its content because my target is to get the "resource_url". I edited my post btw. – Brian Dela Cruz Sep 28 '18 at 10:16
  • I can see the raw request, but whatever code or application is *making* the request is doing it wrong. – ste-fu Sep 28 '18 at 10:28
  • try to use `application/json` and in controller use `FromBody` attribute like `public string Post([FromBody]FormData deviceData)` – er-sho Sep 28 '18 at 12:49
  • I already did that. And What I am getting is a x-www-form-urlencoded Content Type in a form of JSON and FromBody gives me a 415 Status Code Unsupported Media Type. What I stated above is the things I already did. – Brian Dela Cruz Sep 28 '18 at 14:36

1 Answers1

1

The issue, as you are aware, is the Content-Type is application/x-www-form-urlencoded, but what you are posting in the body is json. Unfortunately, just setting the Content-Type of the request isn't adequate because the format x-www-form-urlencoded expects is Variable1=value&Variable2=anotherValue and so on. Here's a slightly releated and fairly interesting answer on x-www-form-urlencoded

For your particular case you will need to perform some kind of conversion from json to a url encoded string

dionm
  • 43
  • 4