2

I have a console app that I'm using to call the CRUD operations of my MVC WebApi Controller.

Currently I have my HTTP request set as follows:

string _URL = "http://localhost:1035/api/values/getselectedperson";

var CreatePersonID = new PersonID
{
    PersonsID = ID
};

string convertedJSONPayload = JsonConvert.SerializeObject(CreatePersonID, new IsoDateTimeConverter());


var httpWebRequest = (HttpWebRequest)WebRequest.Create(_URL);
httpWebRequest.Headers.Add("Culture", "en-US");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "GET";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    streamWriter.Write(convertedJSONPayload);
    streamWriter.Flush();
    streamWriter.Close();
}

return HandleResponse((HttpWebResponse)httpWebRequest.GetResponse());

How do I add the JSON ID parameter to the URL and to be received by my controller 'GetSelectPerson'?

public IPerson GetSelectedPerson(object id)
{
    ....... code
}
0

1 Answer 1

3

You are doing something very contradictory:

httpWebRequest.Method = "GET";

and then attempting to write to the body of the request some JSON payload.

GET request means that you should pass everything as query string parameters. A GET request by definition doesn't have a body.

Like this:

string _URL = "http://localhost:1035/api/values/getselectedperson?id=" + HttpUtility.UrlEncode(ID);

var httpWebRequest = (HttpWebRequest)WebRequest.Create(_URL);
httpWebRequest.Headers.Add("Culture", "en-US");
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "GET";

return HandleResponse((HttpWebResponse)httpWebRequest.GetResponse());

and then your action:

public IPerson GetSelectedPerson(string id)
{
    ....... code
}

Now if you want to send some complex object and use POST, that's an entirely different story.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.