I have built a service with ServiceStack (customer example) as per this link: https://docs.google.com/present/view?id=dg3mcfb_213gsvvmmfk
When I consume it the following way, it works well:
JsonServiceClient client = new JsonServiceClient("http://localhost/RestIntro");
Customer c = new Customer();
c.Name = "myname";
c.Age = 24;
c.Email = "myemail";
var res = client.Post<Customer>("/customers", c);
and it inserts the customer into the database.
But when I test it with Fiddler (POST request), it gives a 201 status message (created) but the database fields remain null, as shown in the following figure:
What could possibly be my issue?
Change the request header:
User-Agent: Fiddler
Host: localhost
Content-Length: 44
Content-Type: application/json
Related
The vscode extension vscode-restclient allows to create http request and handle the response similar to curl or postman.
A POST request to /sales/getResult/ returns this response
HTTP/1.1 200 OK
Date: ....
Content-Type: text/plain; charset=utf-8
Content-Length: 67
Connection: close
Load your results with ID: CJoYTvh8
From the body we need the id CJoYTvh8 to get details. The request to get details goes to the URL /sales/GetResult/{{resultId}} . Where {{resultId}} stands for the ID CJoYTvh8 from the previous response body.
I want to create the request for the details which needs to look like this
#resultId = {{myRequest.response.body}} // this should only be CJoYTvh8
# #name getResult_for_id
POST /sales/getResult/{{resultId}} HTTP/1.1
Host: {{our_host}}
Authorization: Bearer {{authToken}}
Content-Type: application/json
I am looking for something like this
#resultId = response.body.split(':')[1].trim()
Question
How can i split the string Load your results with ID: CJoYTvh8 in vscode-restclient so that i assign only the id CJoYTvh8 to the variable #resultId?
I have a simple REST API that allows to POST resources in a given endpoint, and I want to validate that the response headers are set correctly. I have defined the following feature, which inserts a bunch of users in the database:
Scenario Outline: Creating a bunch of new users when the database is empty
Given header Content-Type = 'application/json'
And request __row
When method post
Then status 201
* match header Content-Type == 'application/json'
* match header Location == 'http://localhost:8080/users/'+response.id
Examples:
| users |
Users are inserted correctly, and in console I can see the following response headers:
1 < 201
1 < Connection: keep-alive
1 < Content-Type: application/json
1 < Date: Mon, 22 Feb 2021 14:06:31 GMT
1 < Keep-Alive: timeout=60
1 < Location: http://localhost:8080/users/user1#test.com
1 < Transfer-Encoding: chunked
But each time I try to run this test, I get the same response:
path: $['Content-Type'][0], actual: null, expected: 'application/json', reason: actual json-path does not exist
Works for me on 0.9.6
* url 'https://httpbin.org/get'
* method get
* match header Content-Type == 'application/json'
But as I mentioned in my comment, please try the 1.0 / RC version: https://github.com/intuit/karate/wiki/1.0-upgrade-guide
As we are focusing efforts on that.
I am trying to create a transient document in adobe sign using rest api.Foloowing is my code snippet:
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.in1.echosign.com/api/rest/v6/transientDocuments');
req.setMethod(postMethod);
req.setHeader('Content-Type', 'application/json');
blob testFileContent = blob.toPDF('test string it is');
// req.setBody('{"filename":"testsign","file":'+testFileContent+'}');
req.setBody('{"fileName":"testsign","file":"'+testFileContent+'"}');
String authorizationHeader = 'Bearer ' +acceessToken;
req.setHeader('Authorization', authorizationHeader);
//req.setHeader('Authorization', acceessToken);
try{
HttpResponse res = h.send(req);
However I am getting the bad request error in response.Issue maybe due to the tag mismatch with adobe sign requirement, however after a lot of research and trial and error I am not able to find the accurate tags to put in JSON for this API request. Any suggestion and help ?
Form data
Content-Disposition: form-data; name="File"; filename="<filename>.pdf"
Content-Type: application/pdf
Request params
Content-Type: multipart/form-data;
Accept: application/json, text/javascript, */*;
More details available in the documentation.
I am writing an API client for Docker. I understood from the documentation that the API is Restful/HTTP, yet if you connect to the local daemon you have to do it over the exposed unix socket.
It all seems to work, I open a socket, send an HTTP request (which respects the specification), I receive the expected response, but also a 400 BAD REQUEST response follows immediately.
Here is the request:
GET /info HTTP/1.1
Host: localhost
Accept: application/json
And here is what I get:
HTTP/1.1 200 OK
Api-Version: 1.30
Content-Type: application/json
Docker-Experimental: false
Ostype: linux
Server: Docker/17.06.1-ce (linux)
Date: Thu, 01 Feb 2018 18:53:18 GMT
Transfer-Encoding: chunked
892
{"ID":"6MGE:35TO:BI..." ...}
0
HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
Connection: close
400 Bad Request
First, I figured that there is a bug on my side and I am somehow sending 2 requests, but I enabled debugging and followed the logs with sudo journalctl -fu docker.service and there is exactly one request received... at least one is logged, the GET /info. I've also debugged the code and 1 single request is sent.
Any hint is greatly appreciated!
Edit: here is the client's code:
final StringBuilder hdrs = new StringBuilder();
for(final Map.Entry<String, String> header : headers) {
hdrs.append(header.getKey() + ": " + header.getValue())
.append("\r\n");
}
final String request = String.format(
this.template(), method, home, hdrs, this.readContent(content)
);
final UnixSocketChannel channel = UnixSocketChannel.open(
new UnixSocketAddress(this.path)
);
final PrintWriter writer = new PrintWriter(
Channels.newOutputStream(channel)
);
writer.print(request);
writer.flush();
final InputStreamReader reader = new InputStreamReader(
Channels.newInputStream(channel)
);
CharBuffer result = CharBuffer.allocate(1024);
reader.read(result);
result.flip();
System.out.println("read from server: " + result.toString());
It seems like you have an extra CRLF between headers and body.
private String template() {
final StringBuilder message = new StringBuilder();
message
.append("%s %s HTTP/1.1\r\n")
.append("Host: localhost").append("\r\n")
.append("%s")
.append("\r\n").append("\r\n") //one of these is superfluous, as each header line ends with "\r\n" itself
.append("%s");
return message.toString();
}
Remove one append("\r\n") after headers and see what happens.
Fixed. Initially, I thought the problem was with the line endings (that they should have been \n instead of \r\n). Turns out, the 400 BAD REQUEST occured because the Connection: close header was missing, while the Request made was being closed right after receiving the response.
More details here.
I'm running into a small roadblock. English is poor, difficulty in expressing
I want to modify the Request in the body and sent two consecutive Request
Original content:
POST http://www.text.com/next?cyt=1 is
HTTP/1.1
Host: www.text.com
User-Agent: Million/1.0.0
Content-Length: 58
Accept: * / *
Accept-Encoding: gzip
Accept-Language:
Content-Type: application / x-www-form-urlencoded
Cookie: S = hveisf76n2lrnpvbeng8ivrat6
Connection: keep-alive
Connection: keep-alive
S = hveisf76n2lrnpvbeng8ivrat6 & step = ashf87afs0a2e4
An attempt was made to modify the Body and send to:
S = hveisf76n2lrnpvbeng8ivrat6 & step = qarker11s77ar
Another attempt to modify the Body and send to:
S = hveisf76n2lrnpvbeng8ivrat6 & step = qarker22s88ar
Request sent twice modified body
I try to do the code:
static function OnBeforeRequest(oSession: Session)
{
if(oSession.uriContains("http://www.text.com/next?cyt=1"))
{
var strBody=oSession.GetRequestBodyAsString();
strBody=strBody.replace("step=ashf87afs0a2e4","step=qarker11s77ar");
oSession.utilSetRequestBody(strBody);
var strBody1=oSession.GetRequestBodyAsString();
strBody1=strBody.replace("step=qarker11s77ar","step=qarker22s88ar");
oSession.utilSetRequestBody(strBody1);
}
}
The question that arises is:
S = hveisf76n2lrnpvbeng8ivrat6 & step = qarker11s77ar
Successful transmission, but
S = hveisf76n2lrnpvbeng8ivrat6 & step = qarker22s88ar
And can not be sent, how to solve this problem? Thank you.
static function OnBeforeRequest(oSession: Session)
{
if(oSession.uriContains("http://www.text.com/next?cyt=1"))
{
var strBody=oSession.GetRequestBodyAsString();
strBody=strBody.replace("step=ashf87afs0a2e4","step=qarker11s77ar");
strBody=strBody.replace("step=qarker11s77ar","step=qarker22s88ar");
oSession.utilSetRequestBody(strBody);
}
}