Comments (10)
mmm, @edi9999 try using CURLOPT_POSTFIELDS. It populates che body post.
#include "curl_easy.h"
#include "curl_header.h"
int main()
{
curl::curl_easy easy;
curl::curl_header header;
std::string a("[{customer:'Franck'}]");
header.add("Content-Type: application/json");
easy.add(curl_pair<CURLoption,curl_header>(CURLOPT_HTTPHEADER, header));
easy.add(curl_pair<CURLoption,string>(CURLOPT_URL, "http://posttestserver.com/post.php"));
easy.add(curl_pair<CURLoption,string>(CURLOPT_POSTFIELDS,a));
try {
easy.perform();
} catch (curl_easy_exception error) {
error.print_traceback();
}
return 0;
}
The server returns the same content you posted in the previous comment, which is:
Time: Sun, 18 Jan 15 13:17:40 -0800
Headers (Some may be inserted by server)
HTTP_CONNECTION = close
REQUEST_URI = /post.php
QUERY_STRING =
REQUEST_METHOD = POST
GATEWAY_INTERFACE = CGI/1.1
REMOTE_PORT = 65188
CONTENT_LENGTH = 21
CONTENT_TYPE = application/json
HTTP_ACCEPT = /
HTTP_HOST = posttestserver.com
UNIQUE_ID = VLwi9NBx6hIAADuQY3cAAAAE
REQUEST_TIME_FLOAT = 1421615860.581
REQUEST_TIME = 1421615860
No Post Params.
== Begin post body ==
[{customer:'Franck'}]
== End post body ==
Upload contains PUT data:
[{customer:'Franck'}]
from curlcpp.
Hi edie999,
What's the content of "a" variable?
from curlcpp.
Hi
this is the declaration of a: string a("[{customer:'Franck'}]");
from curlcpp.
Hi @edi9999 ,
try with this:
curl_form form;
curl_easy easy;
curl_pair<CURLformoption,string> name_form(CURLFORM_COPYNAME,"content");
curl_pair<CURLformoption,string> name_cont(CURLFORM_COPYCONTENTS,"{[customer:'Franck']}");
try {
// Form adding
form.add(name_form,name_cont);
// Add some options to our request
easy.add(curl_pair<CURLoption,string>(CURLOPT_URL,"http://localhost:3000/activePlayers"));
easy.add(curl_pair<CURLoption,curl_form>(CURLOPT_HTTPPOST,form));
easy.perform();
} catch (curl_easy_exception error) {
// Print errors, if any
error.print_traceback();
}
from curlcpp.
Hi Joseph, thanks for the help.
It still doesnt work, here is what does work using standard curl in the command line
curl -i -X POST -H "Content-Type: application/json" -d '[{customer:"Franck"}]' http://localhost:3000/activePlayers
and here is my current code:
curl_form form;
curl_easy easy;
curl_pair<CURLformoption,string> name_form(CURLFORM_COPYNAME,"content");
curl_pair<CURLformoption,string> name_cont(CURLFORM_COPYCONTENTS,tables.dump());
curl_header header;
header.add("Content-Type: application/json");
try {
// Form adding
form.add(name_form,name_cont);
// Add some options to our request
easy.add(curl_pair<CURLoption,curl_header>(CURLOPT_HTTPHEADER, header));
easy.add(curl_pair<CURLoption,curl_form>(CURLOPT_HTTPPOST,form));
easy.add(curl_pair<CURLoption,string>(CURLOPT_URL,"http://localhost:3000/activePlayers"));
easy.perform();
} catch (curl_easy_exception error) {
// Print errors, if any
error.print_traceback();
}
from curlcpp.
Hi @edi9999 ,
Maybe I found another way. I used posttestserver.com to post an example string. I wrote the following code:
#include "curl_easy.h"
int main()
{
curl::curl_easy easy;
std::string postPayload("[{customer:'Franck'}]");
easy.add(curl_pair<CURLoption,string>(CURLOPT_URL,"http://posttestserver.com/post.php") );
easy.add(curl_pair<CURLoption, string>(CURLOPT_POSTFIELDS, "content="+postPayload));
easy.add(curl_pair<CURLoption, bool>(CURLOPT_POST, true));
easy.perform();
return 0;
}
And the server response is:
"Successfully dumped 1 post variables.
View it at http://www.posttestserver.com/data/2015/01/16/10.12.051692677322
Post body was 0 chars long."
That URL contains the following response:
"Post Params:
key: 'content' value: '[{customer:'Franck'}]'
Empty post body.
Upload contains PUT data:
content=[{customer:'Franck'}]"
Can you change the URL with your URL and see if it works? Let me know!
from curlcpp.
Hi this still doesnt work.
I think the problem is that I need to populate the POST BODY, eg here's the expected result of the post :
Time: Sun, 18 Jan 15 02:54:38 -0800
Source ip: 82.244.34.175
Headers (Some may be inserted by server)
HTTP_CONNECTION = close
REQUEST_URI = /post.php
QUERY_STRING =
REQUEST_METHOD = POST
GATEWAY_INTERFACE = CGI/1.1
REMOTE_PORT = 56683
REMOTE_ADDR = 82.244.34.175
CONTENT_LENGTH = 21
CONTENT_TYPE = application/json
HTTP_ACCEPT = */*
HTTP_HOST = posttestserver.com
HTTP_USER_AGENT = curl/7.37.1
UNIQUE_ID = VLuQ7tBx6hIAAEzMsZ0AAAAK
REQUEST_TIME_FLOAT = 1421578478.8785
REQUEST_TIME = 1421578478
No Post Params.
== Begin post body ==
[{customer:"Franck"}]
== End post body ==
Upload contains PUT data:
[{customer:"Franck"}]
I have created that with curl, with the previous command I put in
from curlcpp.
This works now. Thanks a lot for the help:-)
from curlcpp.
Thank you! :)
from curlcpp.
This helped me a lot thanks!
It would be helpful to have this example in the wiki or somewhere.
from curlcpp.
Related Issues (20)
- warnings when building
- NOEXCEPT
- Release:1.2 Cmake fails. HOT 15
- compile warning: invalid use of incomplete type HOT 2
- curl_easy_exception doesn't set any message into std::exception HOT 1
- Build of 1.4 fail with mingw32 in win10 and curl 7.74.0 HOT 12
- Can't link on windows HOT 3
- Singleton initializer issue HOT 1
- replece curlcpp-master/include to curlcpp-master/include/curlcpp
- Can't link with a project with Dynamic CRT (VS2019)
- initialising order warnings HOT 1
- Is a new tag/release coming soon? HOT 1
- build fail with curl 7.83.0 HOT 2
- Static build? HOT 6
- Deprecation warnings with latest curl version
- How to get response content string when using curl_multi HOT 1
- Version inside CMake looks out of date HOT 1
- suggestion libev
- Titles in README.md mismatch
- BUILD_SHARED_LIBS non-standard usage.
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 curlcpp.