Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:59:38+00:00 2026-06-07T04:59:38+00:00

I have created a very simple controller for a REST services using the Spring

  • 0

I have created a very simple controller for a REST services using the Spring Framework. I have no problems using the @PathVariable in conjunction with GETs but when I try to create a POST or PUT, I am unable to curl the function and am trying to figure out where I have gone wrong.

@Controller
public class TestController {


    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public String putName(@RequestParam("name") String name, @RequestParam("value") String value) {

        System.out.println("Name - " + name);
        System.out.println("Value - " + value);


        return "You entered " + name + " with value of " + value;
    }
}

Editing this a bit to include some PHP since that is ultimately where I want to be calling this from.

If I do the following, it works as expected:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/RestService/test');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($ch, CURLOPT_TIMEOUT, 120);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'name=some random name&value=just a value');
    $result = curl_exec($ch);

But if I try to pass the parameters as an array, I receive an HTTP Status 400 -The request sent by the client was syntactically incorrect.

    $data = array
    (
        'name' => 'some random name',
        'value' => 'just a value'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/RestService/test');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($ch, CURLOPT_TIMEOUT, 120);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result2 = curl_exec($ch);

If I can at all help it, I do not want to be building strings, worrying about escaping ampersands, and etc out of the data I want to pass.

Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:59:41+00:00Added an answer on June 7, 2026 at 4:59 am

    Use http_build_query

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I have created a very simple asp.net web service using .NET framework 3.5,
I am using SQL Server 2008 Enterprise. I have created a very simple test
I have created very simple app with persistence context (hibernate as provider) to read
I have created a very simple sharepoint timer job. All i want it to
I have created a very simple GUI project in Qt as follows: main: #include
I have created a GUI for starting a Thread which does something very simple.
I'm trying to create a very simple REST server. I just have a test
I have a very simple model that includes the auto-filled field much like 'created'.
Ok, I have a very simple app created in Grails. I have a generated
I have created a very simple application where I am trying to insert a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.