-1

I am trying to decode the input parameter which is URL encoded.

The input parameter looks like this

json=%7B%0A%22MouseSampleBarcode%22%20%3A%20%22MOS81%22%0A%7D%0A

I have this model class:

public class CoreBarCodeDTO
{
    private string _json;
    public string json
    {
        get { return _json; }
        set
        {
            string decoded = HttpUtility.UrlDecode(value);
            _json = decoded;
        }
    }

}

In my controller I am trying to parse this and trying to retrieve the MouseSampleBarCode:

[HttpPost]
public async Task<IHttpActionResult> Post([FromBody] CoreBarCodeDTO coreBarCode)
{
    string inputJson = coreBarCode.json;

    dynamic results = JsonConvert.DeserializeObject<dynamic>(inputJson);
    string Bar_Code = results.MouseSampleBarcode;

When I try to debug this using fiddler

enter image description here

I am getting this error

System.NullReferenceException: An error has occurred
Object reference not set to an instance of an object.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
xyz
  • 531
  • 1
  • 10
  • 31
  • @Equalsk Yes I did take a look at that thread. But this is something specific to the URL encoding – xyz Dec 27 '17 at 16:12
  • Which line is the error happening on? – Adrian Wragg Dec 27 '17 at 16:15
  • @AdrianWragg when it reaches `string inputJson = coreBarCode.json;` it throws this error – xyz Dec 27 '17 at 16:16
  • 1
    So there's no enough information to help you. Did you put the breakpoint at the `set` method of the `json` property? the `value` variable on it is being setted with the expected string? In wich line of the code the exception was thrown? – Diego Rafael Souza Dec 27 '17 at 16:18
  • I suggest you edit your question with those informations or it will be downvoted – Diego Rafael Souza Dec 27 '17 at 16:19
  • 2
    `But this is something specific to the URL encoding` - No it isn't. I would guess that `coreBarCode` is `null`. – Equalsk Dec 27 '17 at 16:19
  • 1
    @xyz Then you're chasing the wrong problem - and now you can work on getting a minimal, verifiable example that demonstrates the problem you're having. – Adrian Wragg Dec 27 '17 at 16:21
  • @DiegoRafaelSouza I do have a breakpoint in the set method but it doesnot even reach there before throwing the error – xyz Dec 27 '17 at 16:25
  • @Equalsk coreBarCode is null when I debug. I am not sure what is that = I am missing here. – xyz Dec 27 '17 at 16:27
  • 1
    @xyz it mean the problem is in your request. The `Post` method isn't getting a `CoreBarCodeDTO` parameter – Diego Rafael Souza Dec 27 '17 at 16:27
  • @DiegoRafaelSouza Yeah I see that. Should I change the content type in the Fiddler? – xyz Dec 27 '17 at 16:28
  • I guess it should be like `coreBarCode={ json='yourContent'}` but I'm not sure about it. I'll let someone more familiar with json/webrequest to aswer it **after you edit your question and ask for the right problem** – Diego Rafael Souza Dec 27 '17 at 16:33
  • @DiegoRafaelSouza Thanks for your effort I got it working it is just the content type in Fiddler. The code is all good. – xyz Dec 27 '17 at 16:34

1 Answers1

0

Found the issue it is in the Fiddler I gave the wrong Content Type

It should be

Content-Type: application/x-www-form-urlencoded
xyz
  • 531
  • 1
  • 10
  • 31