curl-library
Re: POST problems
Date: Thu, 11 Nov 2004 11:19:17 -0500
Mohun Biswas wrote:
> Andy Hobbs wrote:
>
>> I am creating a buffer (postData) which contains a URI escaped set of
>> parameters (encoded using curl_escape()) and then I try to add this to
>> me request with the following call:
>>
>> code = curl_easy_setopt(handle, CURLOPT_POSTFIELDS, postData);
>>
>> this returns 0, (CURL_OK) so should have succeeded. however when I
>> perform the request a GET is performed and none of my parameters are
>> passed to the server.
>
>
> Can't provide any help except to report a similar and possibly related
> problem! I've noticed that when I tell libcurl to do a POST but don't
> supply a body, it silently converts to a GET. Kind of an annoyance
> though I can work around it ok. I'd love to know if this is a bug or a
> requirement of RFC2616 though.
First, I should apologize for jumping into this thread with an unrelated
issue. I was having a bad night too. But since I started here I think it
makes sense to stay here. Sorry.
I've written up a little test case for my issue (inlined and attached).
The bottom line is, my code follows various paths adding form fields via
curl_formadd(). But there's one code path in which the number of fields
is zero. I still need to make the connection because there's info which
is delivered in the form of parameters too, but in the zero-form-field
case the method is silently switched to GET from POST, presumably
because the 'formpost' pointer is NULL.
In my own case, since I control the server as well as the client, there
are a number of easy workarounds. But it still seems to me that if I ask
for a POST (via CURLOPT_HTTPPOST) it shouldn't be silently switched to a
GET. If I didn't control the server it might be a harder problem to deal
with.
Sample output:
% ./postget 'http://localhost:8080/lab' /etc/motd
Lab Servlet - POST
Param: foo
% ./postget 'http://localhost:8080/lab'
Lab Servlet - GET
Param: foo
As you can see, when I leave off the file for upload the method becomes
GET. I've also included the test servlet from the server side for
illustration purposes.
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "curl/curl.h"
int
main(int argc, char *argv[])
{
char url[2048];
const char *upfile;
CURLcode res;
CURL *curl;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
strcpy(url, argv[1]);
upfile = argv[2];
argc = argc;
strcat(url, "?foo=bar");
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
// Arrange for the upfile to be POST-ed to the server.
if (upfile && !access(upfile, R_OK)) {
curl_formadd(&formpost, &lastptr,
CURLFORM_CONTENTTYPE, "application/text",
CURLFORM_PTRNAME, "--UPFILE--",
CURLFORM_FILE, upfile,
CURLFORM_END);
}
//printf("URL: %s\n", url);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(curl);
return 0;
}
import java.io.*;
import java.util.*;
import javax.servlet.http.*;
public class LabServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.println("Lab Servlet - POST");
Enumeration names = req.getParameterNames();
while (names.hasMoreElements())
out.println("Param: " + names.nextElement());
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.println("Lab Servlet - GET");
Enumeration names = req.getParameterNames();
while (names.hasMoreElements())
out.println("Param: " + names.nextElement());
}
}
- text/x-c-code attachment: postget.c