Comments (7)
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.
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.
many thanks to your quick reply. Problem solved
from fastcgipp.
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.
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.
"\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.
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)
- [failed build example]try use c++20 modules for compile my example
- Support the Meson build system. HOT 1
- Prevent GCC warnings
- Sould not register signal handlers HOT 3
- how to make a json response? HOT 1
- Windows systems not supported??? HOT 1
- 3.1 Release HOT 1
- undefined reference to pthread, for examples HOT 3
- error: ‘Fastcgipp::Http::RequestMethod’ is not a class or namespace
- GLIBC_2.38 not found
- Overview of logging mechanism
- fastcgipp and proxy HOT 1
- CMake PostgreSQL missing required version number HOT 4
- io_uring support? HOT 2
- postgres pipline query HOT 1
- Raspberry Pi with nginx: Echo example gives 413 Request Entity Too Large on form submit HOT 4
- Permission not being set on unix socket according to permission parameter in listen() call. HOT 2
- Error during compilation on Ubuntu 16.04, g++ 5.4 HOT 2
- Missing virtual destructor in Curl_base HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastcgipp.