GithubHelp home page GithubHelp logo

Comments (10)

JosephP91 avatar JosephP91 commented on July 20, 2024 1

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.

JosephP91 avatar JosephP91 commented on July 20, 2024

Hi edie999,
What's the content of "a" variable?

from curlcpp.

edi9999 avatar edi9999 commented on July 20, 2024

Hi

this is the declaration of a: string a("[{customer:'Franck'}]");

from curlcpp.

JosephP91 avatar JosephP91 commented on July 20, 2024

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.

edi9999 avatar edi9999 commented on July 20, 2024

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.

JosephP91 avatar JosephP91 commented on July 20, 2024

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.

edi9999 avatar edi9999 commented on July 20, 2024

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.

edi9999 avatar edi9999 commented on July 20, 2024

This works now. Thanks a lot for the help:-)

from curlcpp.

JosephP91 avatar JosephP91 commented on July 20, 2024

Thank you! :)

from curlcpp.

AstralisSomnium avatar AstralisSomnium commented on July 20, 2024

This helped me a lot thanks!

It would be helpful to have this example in the wiki or somewhere.

from curlcpp.

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.