-
Apr 24th, 2017, 09:15 AM
#1
Thread Starter
Addicted Member
Problems understanding UWP HttpClient POST...
Afternoon all,
I've been researchuing this most of today but can't seem to find many examples and even less that actually make sense...
I have a Web API running on a local machine that has a POST test route on it, works fine from Postman, if I post 'key Test' and 'Value Blarblarblar' then it returns "{ "test": "blarblarblar"}".
Desperately trying to get UWP VB to do the same but don't seem to be able to make any headway.
Code:
Dim Content As HttpContent = New StringContent("test test test")
Using http As New HttpClient()
Using response As HttpResponseMessage = Await http.PostAsync("http://192.168.1.103/api/posttest", Content)
Dim result = Await response.Content.ReadAsStringAsync()
Using ms = New MemoryStream(Encoding.UTF8.GetBytes(result))
End Using
End Using
End Using
I'm not getting any error messages, in the responce I'm getting a 'status code:200' and a 'reason phrase:OK'. I have a sneaky feeling i shouldn't just be sending 'test test tets' I think I should be sending a key and a value, I just can't work out how.... any ideas or pointers...?
Thanks
Dave
Last edited by QuattroDave; Apr 24th, 2017 at 10:07 AM.
Reason: typo
-
Apr 24th, 2017, 09:33 AM
#2
Re: Problems understanding UWP HttpClient POST...
It really depends on what your WebAPI Post method is expecting.
Can you post your WebAPI method here so we can see it?
Also it would be helpful if you could tell us what your actually trying to achieve?
do you just want to return a response ok or error? or do you want to return something more?
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Apr 24th, 2017, 09:45 AM
#3
Thread Starter
Addicted Member
Re: Problems understanding UWP HttpClient POST...
Thanks for your reply,
Sure thing, the Web API is written in Laravel / PHP, the api route code is:
Code:
Route::post('posttest', ['uses' => 'TestController@posttest']);
the controller code is
Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function posttest(Request $Request)
{
return $Request->all();
}
}
To be honest I'm just trying to learn how it works and how to make requests with UWP & VB.net. For the time being I hoping I can get it to replicate Postman. Hopefully once I can get the VB.net POST working I can go on to make it actually do something interesting...
-
Apr 24th, 2017, 10:06 AM
#4
Re: Problems understanding UWP HttpClient POST...
Looking at that screenshot I suspect the API is expecting a string of
Code:
{
"test" : "blarblarblar"
}
-
Apr 24th, 2017, 11:01 AM
#5
Thread Starter
Addicted Member
Re: Problems understanding UWP HttpClient POST...
Originally Posted by PlausiblyDamp
Looking at that screenshot I suspect the API is expecting a string of
The API should reply with whatever it was sent. Just a thought, when doing a POST
Code:
Using response As HttpResponseMessage = Await http.PostAsync("http://192.168.1.103/api/posttest", Content)
I am able to get a response arn't i....? I was hoping to be able to use a custom responce to know whether the operation was a sucess or fail...?
Thanks
Dave
-
Apr 24th, 2017, 04:46 PM
#6
Re: Problems understanding UWP HttpClient POST...
For the API to respond correctly it still needs the data it receives to be in the expected format, the screenshot you posted shows Postman sending Json with a particular structure - I would think that if it works with that particular Json structure but doesn't work without that Json structure then it is at least worth trying to send data that matches that Json structure.
Have you tried sending the same string literal via code as you did through Postman?
-
Apr 25th, 2017, 03:43 AM
#7
Re: Problems understanding UWP HttpClient POST...
Yes i agree with Mr Damp
your sending your string in plain text.
adding it to a httpcontent does not change that
Code:
Dim Content As HttpContent = New StringContent("test test test")
its still in plain text
you need to do something like this to replicate Postman
Code:
Dim Content As HttpContent = New StringContent("{"test" : "blarblarblar""})
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Apr 25th, 2017, 03:44 AM
#8
Re: Problems understanding UWP HttpClient POST...
if you want to send "test test test" instead then it should be - {"test" : "test test test"}
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Apr 25th, 2017, 08:28 AM
#9
Thread Starter
Addicted Member
Re: Problems understanding UWP HttpClient POST...
Thanks for the replys guys,
After a lot of head scratching I think we've got to the bottom of it. I'm pretty sure the problem was with the content creation. You were correct the first part was the string needed to be in a JSON format, had problem manually typing it so used a serializer. The second was that for some reason it needed "application/json" at the end of the content string.
Full code:
Code:
Dim output As String = Nothing
'JSON Serialize the content string
Dim ms As MemoryStream = Nothing
Dim Serializer As New DataContractJsonSerializer(NewNotes.GetType())
ms = New MemoryStream()
Serializer.WriteObject(ms, NewNotes)
ms.Seek(0, SeekOrigin.Begin)
Dim sr As New StreamReader(ms)
Dim postbody As String = sr.ReadToEnd()
Try
Dim url As String = "http://192.168.1.103/api/posttest"
Dim Content As HttpContent = New StringContent(postbody, Encoding.UTF8, "application/json")
Using http As New HttpClient()
Using response As HttpResponseMessage = Await http.PostAsync(url, Content)
output = Await response.Content.ReadAsStringAsync()
End Using
End Using
Dim messdialogue = New MessageDialog("Output: " & output)
Await messdialogue.ShowAsync()
Catch ex As System.Net.Http.HttpRequestException
Dim messdialogue = New MessageDialog(ex.InnerException.Message)
End Try
Last edited by QuattroDave; Apr 25th, 2017 at 08:34 AM.
Reason: typo
-
Apr 25th, 2017, 08:41 AM
#10
Re: Problems understanding UWP HttpClient POST...
The second was that for some reason it needed "application/json" at the end of the content string.
Ah yes it will need that, you need to tell it the type of data your sending as it could just as well be XML.
had problem manually typing it so used a serializer.
I would still seriously take a look at NewtonSoft Json for your serialisation, you can get it from NuGet and it just a much nicer library for serialisation.
Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you
-
Apr 25th, 2017, 08:50 AM
#11
Thread Starter
Addicted Member
Re: Problems understanding UWP HttpClient POST...
Originally Posted by NeedSomeAnswers
I would still seriously take a look at NewtonSoft Json for your serialisation, you can get it from NuGet and it just a much nicer library for serialisation.
Ah yes you did mention NewtonSoft Json in a previous thread, I'll have a look into it. Have you got any good examples for it?
Thanks
Dave
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|