Code Monkey home page Code Monkey logo

Comments (7)

eddic avatar eddic commented on June 3, 2024

If you insert data into the Curl object using the stream insertion operator (<< ) it gets sent as a POST request. The curl object doesn't use the easy setopt you're looking at. It uses a callback function that extracts the data from a non-contiguous buffer. Just follow what the above code is doing. It gets sent as a POST request.

from fastcgipp.

hengbenkeji avatar hengbenkeji commented on June 3, 2024

Thank you for your quick reply. I kept trying using curl << REQUEST_BODY and finally it worked.
The following code runs on the server, and can send JSON request to a third SMS server.

void OpenAPIHandler::response_get_info5( RequestPtr request ){
int id = 1;
std::setlocale( LC_ALL, "en_US.utf8" );
const auto initialFds = openfds();
{
Fastcgipp::Curler curler( 1 );
curler.start();
std::stringstream ss;
std::vectorstd::string proper;
Fastcgipp::Curl curl;
curl.setCallback( std::bind( &OpenAPIHandler::booking_callback, this, request, curl, proper, std::placeholders::_1 ) );
curl.setUrl("http://yourdomain/echo.cgi");
curl.addHeader("Content-Type: application/json; charset=utf-8");
curl.addHeader("Accept-Charset: utf-8");
std::string requestBody = std::string( "\r\n\r\n{\r\n\r\n"a":1\r\n\r\n}" );
curl << requestBody << id++;
curler.queue( curl );
curler.stop();
curler.join();
}
}

from fastcgipp.

hengbenkeji avatar hengbenkeji commented on June 3, 2024

many thanks to your quick reply. Problem solved

from fastcgipp.

eddic avatar eddic commented on June 3, 2024

By the way you shouldn't need to set the content-length header. Curl should set that automatically based on the size passed to curl. This is at least what my tests have indicated. If you should happen to notice otherwise let me know.

from fastcgipp.

hengbenkeji avatar hengbenkeji commented on June 3, 2024

It seems that I closed this post too soon. My SMS server could not recognize the request JSON as shown below. So I used https://postman-echo.com/post to debug the information. In the following code, I did not set the Content-Length, and I used application/json, and sent request to the API https://postman-echo.com/post.

void OpenAPIHandler::response_get_info5( RequestPtr request ){
int id = 1;
std::setlocale( LC_ALL, "en_US.utf8" );
const auto initialFds = openfds();
{
Fastcgipp::Curler curler( 1 );
curler.start();
std::stringstream ss;
std::vectorstd::string proper;
Fastcgipp::Curl curl;
curl.setCallback( std::bind( &OpenAPIHandler::booking_callback, this, request, curl, proper, std::placeholders::_1 ) );
curl.setUrl("http://yourdomain/echo.cgi");
curl.addHeader("Content-Type: application/json; charset=utf-8");
curl.addHeader("Accept-Charset: utf-8");
std::string requestBody = std::string( "\r\n\r\n{\r\n\r\n"a":1\r\n\r\n}" );
curl << requestBody << id++;
curler.queue( curl );
curler.stop();
curler.join();
}
}

The API https://postman-echo.com/post responded as follows, failing to recognize the simples json {"a":1} as a json object(json":null").

response_data: {"args":{},"data":"\r\n\r\n0\r\n\r\n{\r\n\r\n"a":1\r\n\r\n}1","files":{},"form":{},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-5ffbad5b-1fea46516f9a6aef388bed9a","content-length":"25","accept":"/","content-type":"application/json; charset=utf-8","accept-charset":"utf-8"},"json":null,"url":"https://postman-echo.com/post"}

I tried many times, including jsoncpp Json::Value, but with no luck. Would you explain how to format the data so as to let the server recognize the request body is a JSON object?

Thanks in advance

from fastcgipp.

eddic avatar eddic commented on June 3, 2024

"\r\n\r\n0\r\n\r\n{\r\n\r\n"a":1\r\n\r\n}1" is absolutely not valid JSON. There is a number one at the end. This is coming the id++. I'm unclear why that's been left in. You may be inclined to read up on iostream usage.

from fastcgipp.

hengbenkeji avatar hengbenkeji commented on June 3, 2024

You are right, sir. Now the server can recognize I am sending a JSON request.
Thank you for your quick reply.
Many thanks

from fastcgipp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.