简体   繁体   中英

Can't replicate POST request in CasperJS

I am trying to do a POST request from CasperJS:

 for(var i in phantom.cookies)
    cookie += phantom.cookies[i].name + '=' + phantom.cookies[i].value + ';';

 casper.thenOpen(url, {
    method: 'post',
    data: {
        // some form-data
     },
    headers: {
        'Content-Type': 'multipart/form-data',
        'Cookie': cookie
    }
 }, function(response) {
    if(response.status == 200) {
        require('utils').dump(this.page.content);
    }
 });

When I execute the request somewhere else I get the appropriate response, but for some reason I can't replicate the output on CasperJS. Here is an example of a cURL request that did work:

curl -X POST -H "Cookie: cookie" -H "Content-Type: multipart/form-data" -F "field=field" "url"

My case was CasperJS (1.1.x version) testing my REST API. API is written in node. In one specific resource I'm processing a form using formiddable. When I set content-type: multipart/form-data in Casper, that formiddable library complains with this error: bad content-type header, no multipart boundary

I could not find a solution for this. Instead I ended up doing this in Casper:

casper.open(<url>, {
        method: 'POST',
        data: "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\nxxx\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"upload\"\r\n\r\nhttps:\/\/url\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
        headers: {
            'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
        }
    })

basically I formatted the body to the proper format and this worked

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM