-
-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Unable get response from wiremock.net server in c# #213
Comments
Which Framework do you use ? |
Target framewok: |
Solution: class Program
{
static void Main(string[] args)
{
RunAsync().GetAwaiter().GetResult();
}
static async Task RunAsync()
{
var server = FluentMockServer.Start();
server
.Given(Request.Create().WithPath("/product/p0001").UsingGet())
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}"));
var getResp = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/product/p0001");
Console.WriteLine(getResp);
}
} |
hi @StefH , Thanks for reply, Its working fine, For POST method , How can i check POST's response in our c# code, Can you please share the some example .json files for POST method, Thanks! |
Hi @StefH I used below code, to get post's response , StringContent content = new StringContent(JsonConvert.SerializeObject(ob), Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("api/products/save", content);
string content = await response.Content.ReadAsStringAsync(); Thanks! |
Sorry. I couldn't get you ...
Thanks & Regards
Elangopalakrishnan.V
…On Oct 16, 2018 21:15, "Stef Heyenrath" ***@***.***> wrote:
That would be:
string content = await response.Content.ReadAsStringAsync();
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#213 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AZpriQ4UnLjYQ9LZTOKyWm_4iQHv3367ks5ulf8SgaJpZM4XePnA>
.
|
BTW you can use on github issues: triple single quotes space C# ( |
AH, you need a JSON mapping example. Something like this? {
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "api/products/save"
}
]
},
"Methods": [
"post"
]
},
"Response": {
"BodyAsJson": { "body": "static mapping" },
"Headers": {
"Content-Type": "application/json"
}
}
} |
BTW you can also use the gitter channel for asking questions. |
@StefH , I have two .json files one is for request and one is for response, String requestpath = @"C:\Users\req.json";
String req = n.GetJSONObjectFromFile(requestpath);
String responsePath = @"C:\Users\resp.json";
String resp = n.GetJSONObjectFromFile(responsePath);
Requests ob = JsonConvert.DeserializeObject<Requests>(req);
Resp obresp = JsonConvert.DeserializeObject<Resp>(resp);
StringContent content = new StringContent(JsonConvert.SerializeObject(ob), Encoding.UTF8, "application/json");
server
.Given(
Request.Create()
.WithPath("/emailValidate")
.WithBody(new JsonMatcher(JsonConvert.SerializeObject(ob))).UsingPost())
.RespondWith(
Response.Create()
.WithStatusCode(200)
.WithHeader("Constent-Type", "application/json")
.WithBody(resp));
HttpResponseMessage response = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/emailValidate", content);
Console.WriteLine(JsonConvert.SerializeObject(obresp)); req.json {
"Email":"[email protected]"
} resp.json {
"isValid":true,
"certainty":"Verified"
} But i don't know , how i need to match and all, i saw above site, but i don't understand why we are using matchers. Thanks |
You need to use use a matcher for the body to make sure that only a request with a specific body is matched. So on your case you can use code like: var server = FluentMockServer.Start();
server
.Given(Request
.Create()
.WithPath("/jsonmatcher1")
.WithBody(new JsonMatcher("{ \"Email\": \"[email protected]\" }"))
.UsingPost())
.WithGuid("debaf408-3b23-4c04-9d18-ef1c020e79f2")
.RespondWith(Response.Create().WithBody(@"{ ""isValid"": ""true"" }")); |
Hi , I'm using same code, and getting response like below, But i'm getting error 500. server Thanks! |
Hi , I'm using same code, and getting response like below, But i'm getting error 500. server Thanks! Sorry , by mistake closed :( |
Can you please review StringContent cnent = new StringContent(JsonConvert.SerializeObject("{ "Email": "[email protected]" }"), Encoding.UTF8, "application/json"); These codes are correct?? Thanks |
use var stringContent1 = new StringContent("{ \"Email\": \"[email protected]\" }", Encoding.UTF8, "application/json");
var postResponse1 = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/jsonmatcher1", stringContent1);
string postContent1 = await postResponse1.Content.ReadAsStringAsync();
Console.WriteLine(postContent1);
// or
var stringContent2 = new StringContent(JsonConvert.SerializeObject(new { Email = "[email protected]" }), Encoding.UTF8, "application/json");
var postResponse2 = await new HttpClient().PostAsync("http://localhost:" + server.Ports[0] + "/jsonmatcher1", stringContent2);
string postContent2 = await postResponse2.Content.ReadAsStringAsync();
Console.WriteLine(postContent2); |
Hi ,
Problem:
I'm trying to implement the wiremock in c#, For that I have downloaded the wiremock .net library from NuGet,
And I'm trying to do GET method , but i'm getting below error message :(
From C#
"Id = 17, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}""
if i use "await" - It is waiting for long time then it is returning 404 file not found error,
From Post Man
Versions of packages:
WireMock.Net - 1.0.3.16
System.Net.Http - 4.3.3
Code snippet:
Please advice me what I'm missing here :(
Thanks in advance!
Elango
The text was updated successfully, but these errors were encountered: