Home > Blockchain >  How to encode JSON Url in ASP.NET Core
How to encode JSON Url in ASP.NET Core

Time:10-21

I want to make a post to an API, and the link I'm using to make the post call has some URL parameters.

Link to make post: http://someservice/api/v1/requests?input_data=encoded_data

The parameter (input_data) is a JSON that needs to be encoded before. When I use this enter image description here

then I found you made a mistake in this place, you should remove what I marked in the screenshot below. enter image description here

CodePudding user response:

Instead of writing JSON as a string, would suggest setting the data as object with POCO class or anonymous type. And next, serialize it to a JSON string.

Benefit: Avoid JSON syntax errors.

anonymous type

var json = new {
    request = new {
        requester = new {
            email_id = model.Requester
        }
    },
    subject = model.Subject,
    description = model.Description
};
        
Console.WriteLine(JsonConvert.SerializeObject(json));

Sample program

Output

{"request":{"requester":{"email_id":"Requester"}},"subject":"Subject","description":"Description"}
  • Related