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);
}
}
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 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 would like to upload a log file from our iOS app directly to a folder on Box.com. Reading the api for Box this functionality is provided with REST webservices. The examples in the api are cURL which work out of the box (sorry, no pun intended) from the terminal.
Our app is Swift based, and I am using NSURLSession to make the request. After too many attempts to count, and close examination of the limited blogs on this subject (iOS / Box) I still have not managed to upload a test file.
One frustrating thing is that the api appears to be wrong, as seen here, but following the format suggested in this answer did not help.
Here is the NSURLSession request code from my latest attempt:
let path = NSBundle.mainBundle().pathForResource("testFile", ofType: "txt")
let data: NSData? = NSData(contentsOfFile: path!)
let url = NSURL(string: "https://upload.box.com/api/2.0/files/content")!
let request = NSMutableURLRequest(URL: url)
let boundary = generateBoundaryString()
request.HTTPMethod = "POST"
request.addValue("Bearer q1ckXDSTC0EcsCJiLtW638o5x5roWabb", forHTTPHeaderField: "Authorization")
let body = NSMutableData()
body.appendData("Content-Type: multipart/form-data; boundary=\(boundary)\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("--\(boundary)\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition: form-data; name=\"parent_id\"\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Type: text/plain\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("0\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("--\(boundary)\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Disposition: form-data; name=\"filename\"; filename=\"testFile.txt\"\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData("Content-Type: text/plain\n".dataUsingEncoding(NSUTF8StringEncoding)!)
body.appendData(data!)
body.appendData("\n--\(boundary)--".dataUsingEncoding(NSUTF8StringEncoding)!)
request.HTTPBody = body
let task = NSURLSession.sharedSession().uploadTaskWithRequest(request, fromData: data!, completionHandler: {(data,response,error) in
if(error != nil) {
NSLog("Error message = \(error.debugDescription)")
}
if(response != nil) {
NSLog("Response = \(response)")
}
if(data != nil) {
NSLog("Data = \(data!)")
}
});
task.resume()
The server responds with a 405 error.
Response = Optional(<NSHTTPURLResponse: 0x157de26d0> { URL: https://upload.box.com/api/2.0/files/content } { status code: 405, headers {
Age = 0;
Allow = "GET, OPTIONS, HEAD";
Connection = "keep-alive";
"Content-Length" = 0;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Thu, 13 Oct 2016 14:49:49 GMT";
Server = ATS;
} })
From the example above the request header is:
Request headers - Optional(["Authorization": "Bearer q1ckXDSTC0EcsCJiLtW638o5x5roWabb"])!
And the request body is:
body - Content-Type: multipart/form-data; boundary=0B1D5883-9EA6-4D6B-9FEC-C51D817D5975
--0B1D5883-9EA6-4D6B-9FEC-C51D817D5975
Content-Disposition: form-data; name="parent_id"
Content-Type: text/plain
0
--0B1D5883-9EA6-4D6B-9FEC-C51D817D5975
Content-Disposition: form-data; name="filename"; filename="testFile.txt"
Content-Type: text/plain
Hello
--0B1D5883-9EA6-4D6B-9FEC-C51D817D5975--
We have tried to disseminate the cURL request with Wireshark but as it is an SSL request this didn't help much.
The verbose version of the cURL request looks like this:
0000: POST /api/2.0/files/content HTTP/1.1
0026: Host: upload.box.com
003c: User-Agent: curl/7.47.0
0055: Accept: */*
0062: Authorization: Bearer o6ojjxpfECdKrBzIUhiRyRKHTTXg8Sll
009a: Content-Length: 368
00af: Expect: 100-continue
00c5: Content-Type: multipart/form-data; boundary=--------------------
0105: ----ed667cff7b6570a1
011b:
== Info: Done waiting for 100-continue
=> Send data, 285 bytes (0x11d)
0000: --------------------------ed667cff7b6570a1
002c: Content-Disposition: form-data; name="attributes"
005f:
0061: {"name":"testFile.txt", "parent":{"id":"0"}}
008f: --------------------------ed667cff7b6570a1
00bb: Content-Disposition: form-data; name="file"; filename="testFile.
00fb: txt"
0101: Content-Type: text/plain
011b:
=> Send data, 35 bytes (0x23)
0000: This is a test file.
=> Send data, 48 bytes (0x30)
0000:
0002: --------------------------ed667cff7b6570a1--
Which we have also tried to replicate exactly, but the problem remains.
If anyone has achieved this successfully I would really appreciate some direction or suggestions
We contacted Box support team but they have yet to respond.
how can I use Fiddler to pass 2 parameters to a method with a signature like this :
[WebInvoke(UriTemplate = "Login", Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
bool Login(string login, string password);
Any suggestion to change something in the method is welcome, but I have always to pass 2 string parameters.
It is much easier to the services with WCFTestClient. Anyway, Fiddler has composer functionality which can be used to test any http calls.
Request Headers should look like below. Just change SOAPAction accordingly.
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/IService/Login"
Host: localhost:2045
Content-Length: 179
Expect: 100-continue
Accept-Encoding: gzip, deflate
Proxy-Connection: Keep-Alive
And request body:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><Login xmlns="http://tempuri.org/"><login>sdf</login><password>sdf</password></Login></s:Body></s:Envelope>
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