如何使用curl发送参数?

时间:2016-01-14 08:48:03

标签: java curl arguments

我正在尝试在执行java spring项目时添加参数。

案例)Java独立:java -cp target/A.jar myProgram arg1 arg2 arg3 arg4 - >的务!

SPRING-RESTful API案例怎么样?
没有参数,我用了 curl -H "Accept: application/json" -H "X-AUTH-TOKEN: XXXX" -X POST -d "[1234,4567,89012]" http://my.ip.address:port/myProgram

那么,如何在curl命令中包含arg1,arg2,arg3,arg4?

GreetingController.java

package Rest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import java.nio.charset.Charset;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

import myProgram;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;


//
@RestController
public class GreetingController {
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

//    @Autowired
//    bboxevolutionTripleFromDB processor;


    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }


    @RequestMapping(value="/myProgram", method = RequestMethod.POST)
    public ResponseEntity<String> doDemo(@RequestHeader("X-AUTH-TOKEN") String authToken) {

        try {
            TypeInferenceMain.doDemo(null);
            StringBuffer sb = new StringBuffer("[");

            sb.append("]");
            HttpHeaders responseHeaders = new HttpHeaders();
            MediaType mediaType = new MediaType("application", "json", Charset.forName("UTF-8"));
            responseHeaders.setContentType(mediaType);
            return  new ResponseEntity(sb.toString(), responseHeaders, HttpStatus.OK);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        HttpHeaders responseHeaders = new HttpHeaders();
        MediaType mediaType = new MediaType("application", "json", Charset.forName("UTF-8"));
        responseHeaders.setContentType(mediaType);

        return new ResponseEntity("[]", responseHeaders, HttpStatus.OK);
    }

}

1 个答案:

答案 0 :(得分:0)

我需要更多信息以确定参数使用的编码类型,但它是这样的:

curl -X POST -H "Accept: application/json" -H "X-AUTH-TOKEN: token" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 88fd44ab-692a-bef0-c80e-fe8b98c33bd1" -d '[arg1][arg2][arg3][arg4]' 'http://my.ip.address:port/myProgram'