Quick question that I'm struggling to figure out.
I've got a REST Api up and running that works fine with Postman which I'm trying to figure out how to call with a Post-request from Xamarin Forms (using HTTPClient).
This is my current code:
public CreateFoundation()
{
InitializeComponent();
lblInstructions.FontAttributes = FontAttributes.Bold;
lblInstructions.FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label));
btnCreate.Clicked += async (s, e) =>
{
var arb = await PostCreateFoundation(txtCreateFoundation.Text);
await DisplayAlert("Clicked", "I was clicked. Text was: " + arb, "OK");
};
}
public async Task<string> PostCreateFoundation(string arbitrary)
{
Debug.WriteLine("I'm in!");
var client = new System.Net.Http.HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
var content = new StringContent(arbitrary);
var response = await client.PostAsync("http://myapi.com/RestfulAPI/api/Ole_foundations/Insert/", content);
var result = (response.Content.ReadAsStringAsync().Result);
return result;
}
When displaying the returned string
( arb
) I can see that the uri is http://myapi.com/RestfulAPI/api/Ole_foundations/Insert/.
, regardless of what I send as the arbitrary string...
The resulting url when sending the string "test" is:
{MESSAGE": "No HTTP resource was found that matches the request URI ' http://myapi.com/RestfulAPI/api/Ole_foundations/Insert/ '."}
EDIT: The API takes an arbitrarily string as a token, adds a predefined object to the database and returns the passed in arbitrary string as a return token.
REST API function header:
[HttpPost]
public string Insert(string s)
When an action parameter is a simple type like a string, WebAPI assumes it should look for it in the query string. But you are passing it in the body. Try changing the method signature on the API side to:
public string Insert([FromBody]string s)
Remove the final "/" on the uri
Should be:
Also what Todd Meiner said
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.