4

I'm trying to pass a lengthy string through my POST methond from the actual body, it works perfectly fine if I pass it through url but I dont know what to change so I can insert data from body instead.

public void PostMethod(string id, [FromBody]string data)
    {
        if (ModelState.IsValid)
        {
            var result = client.Store(StoreMode.Add, id, data);
        }
        else
        {
        }

    }

if I use it like this:

http://localhost:8888/api/data?id=2&data=MybigString

It works perfectly, but I don't want to pass data from URL, any suggestion would be highly appreciated.

enter image description here

2 Answers 2

4

Given your action method, which is public void PostMethod(string id, [FromBody]string data), you can use the URI of http://localhost:8888/api/data/2 and the message body of =MyBigString. If you use jQuery, you can use something like this: $.post('api/data/2', { '': 'MyBigString' }); to ensure the correct message body is sent.

EDIT:

enter image description here

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

6 Comments

how would I use the JQuery to add some data?
How are you calling the web API now?
Im using Chrome Plug Ins , called POSTMAN, or SOAP UI
In POSTMAN, add a header Content-Type: application/x-www-form-urlencoded. Select "raw" and enter =MyBigString in the textbox. For URL, use http://localhost:8888/api/data/2.
I just uploaded the screenshots, would you please take a look at it and tell me what Im doing wrong
|
0

Use a hidden field and set the value of it before the post. Just make sure it's inside your form.

@Html.HiddenFor(Model.data)

To set the value in javascript using jQuery:

$("#data").val('my big string');

Alternatively, if you're not binding to a strongly-typed view, a simple HTML hidden input will work instead of the @Html.HiddenFor():

<input type="hidden" id="data" name="data" />

2 Comments

would you please be little more specific, exactly where I need to modify, I'm programming in .net and if you are familiar with it, it does auto generates so many classes when you create a web api
I dont know... you tell me. Is that the View that's making the call to the API method?

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.