curl 사용법

|

CURL 사용방법

참고 : https://gist.github.com/subfuzion/08c5d85437d5d4f00e58

Common Options

-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.

-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).

-c, --cookie-jar <file name> File to save response cookies to.

-d, --data <data> Send specified data in POST request. Details provided below.

-f, --fail Fail silently (don’t output HTML error form if returned).

-F, --form <name=content> Submit form data.

-H, --header <header> Headers to supply with request.

-i, --include Include HTTP headers in the output.

-I, --head Fetch headers only.

-k, --insecure Allow insecure connections to succeed.

-L, --location Follow redirects.

-o, --output <file> Write output to . Can use `--create-dirs` in conjunction with this to create any directories specified in the `-o` path.

-O, --remote-name Write output to file named like the remote file (only writes to current directory).

-s, --silent Silent (quiet) mode. Use with -S to force it to show errors.

-v, --verbose Provide more information (useful for debugging).

-w, --write-out <format> Make curl display information on stdout after a completed transfer. See man page for more details on available variables. Convenient way to force curl to append a newline to output: -w "\n" (can add to ~/.curlrc).

-X, --request The request method to use.

POST

When sending data via a POST or PUT request, two common formats (specified via the Content-Type header) are:

  • application/json
  • application/x-www-form-urlencoded

Many APIs will accept both formats, so if you’re using curl at the command line, it can be a bit easier to use the form urlencoded format instead of json because

  • the json format requires a bunch of extra quoting
  • curl will send form urlencoded by default, so for json the Content-Type header must be explicitly set

This gist provides examples for using both formats, including how to use sample data files in either format with your curl requests.

curl usage

For sending data with POST and PUT requests, these are common curl options:

  • request type
    • -X POST
    • -X PUT
  • content type header
  • -H "Content-Type: application/x-www-form-urlencoded"
  • -H "Content-Type: application/json"

  • data
    • form urlencoded: -d "param1=value1&param2=value2" or -d @data.txt
    • json: -d '{"key1":"value1", "key2":"value2"}' or -d @data.json


Examples

POST application/x-www-form-urlencoded

application/x-www-form-urlencoded is the default:

curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/data

explicit:

curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data

with a data file

curl -d "@data.txt" -X POST http://localhost:3000/data

POST application/json

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data

with a data file

curl -d "@data.json" -X POST http://localhost:3000/data

data.json

{
  "key1":"value1",
  "key2":"value2"
}

data.txt

param1=value1&param2=value2

package.json

{
  "name": "postdemo",
  "version": "1.0.0",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "body-parser": "^1.15.0",
    "express": "^4.13.4"
  }
}

server.js

var app = require('express')();
var bodyParser = require('body-parser');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

app.post('/data', function (req, res) {
  console.log(req.body);
  res.end();
});

app.listen(3000);

BroadcastReceiver 코딩 템플릿

|

BroadcastReceiver 코딩 템플릿

다음은 BroadcastReceiver 프로그래밍 템플릿입니다. register/unregister를 BroadcastReceiver 외부에서 하는 방법도 있는데, 개인적으로는 해당 클래스 내부에 하는 것이 결합도는 낮아지고 응집력도 더 높아지며 코드도 깔끔해지는 것 같습니다.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class EventBroadcastReceiver extends BroadcastReceiver {

  static final String ACTION_EVENT_DEBUG_ON = "ACTION_EVENT_DEBUG_ON";
  static final String ACTION_EVENT_DEBUG_OFF = "ACTION_EVENT_DEBUG_OFF";

  final Context context;

  public EventBroadcastReceiver(Context ctx) {
    this.context = ctx;
  }

  public void init() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(ACTION_EVENT_DEBUG_ON);
    filter.addAction(ACTION_EVENT_DEBUG_OFF);

    context.registerReceiver(this, filter);
  }

  public void fin() {
    context.unregisterReceiver(this);
  }

  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (ACTION_EVENT_DEBUG_ON.equals(action)) {
      // TODO
    } else if (ACTION_EVENT_DEBUG_OFF.equals(action)) {
      // TODO
    }
  }
}

mp3 파일을 ogg 파일로 변경하는 방법

|

리눅스에서 mp3 파일을 ogg 파일로 변경하는 방법

리눅스(Ubuntu)에서 mp3 파일을 ogg 파일로 변경하는 방법은 다음과 같습니다.

sudo apt-get install dir2ogg

dir2ogg -r /path/to/mp3s/

apt-get -f install 오류 메시지나는 경우

|

apt 명령어 실행시 apt-get -f install 오류 발생하는 경우

apt 명령어를 실행할 때마다 아래와 같은 오류 메시지가 계속 발생하는 경우가 있을 수 있습니다.

You might want to run 'apt-get -f install' to correct these.
The following packages have unmet dependencies:
 pcl : Depends: libvtk6.2 but it is not installed
       Depends: libvtk6.2-qt but it is not installed
E: Unmet dependencies. Try using -f.

이 경우는 다음 명령어로 해결할 수 있습니다.

<pre class=”prettyprint” sudo apt –fix-broken install sudo apt-get update sudo apt-get upgrade </pre>

Git Submodule 사용 방법

|

Git Submodule 사용 방법

Submodule을 추가하려는 부모를 parent라고 하고, Submodule로써 추가되려는 자식을 child라고 가정할 때, 다음과 같이 사용할 수 있습니다.


git submodule 추가 방법

아직 parent에 submodule이 추가되지 않은 상태에서 다음 명령어를 입력하여 submodule 추가를 해줄 수 있습니다.

git clone git@github.com:snowdeer/parent.git

cd parent
git submodule add git@github.com:snowdeer/child.git child
git commit -m "submodule is added."
git push

이 때, 별도로 git add 절차 없이 바로 commit 할 수 있습니다.


git clone으로 parent를 가져왔을 때

git clone으로 parent를 가져왔을 때, 내부의 child는 디렉토리만 만들어져 있고 내부가 없습니다. 이 때 submodule 초기화 및 업데이틀 해 주어야 합니다. 루트에서 다음 명령어를 실행하면 됩니다.

git submodule init

git submodule update

다만, 이 때 submodule의 소스 버전은 최신 버전을 가리키는 것이 아니라, submodule add를 수행했을 때의 버전을 가리키고 있습니다. submodule은 리파지토리가 실제로는 분리되어 있기 때문에 각 모듈의 버전이 따로 관리되는데, parent 프로젝트에서는 현재 submodule의 버전이 최신인지 아닌지 신경쓸필요없이 안정적인 특정 버전만 가리키면 되기 때문에 프로젝트 배포 등에서는 관리가 수월한 장점이 있습니다. 물론, 개발중인 프로젝트에서는 각 submodule들을 최신 버전으로 유지해야 할 경우 각 submodule들의 업데이트를 수동으로 한 번씩 더 해줘야 하는 단점이 있기도 합니다.


submoudle 최신 버전으로 교체

submodule을 최신 버전으로 교체하는 방법은 다음 같습니다.

  • child 디렉토리에 들어가서 각각의 submodule들을 개별 업데이트 해주는 방식. 각 submodule 디렉토리에서 git pull 명령어나 git checkout 명령어 등을 이용해서 업데이트 가능.
  • parent내에서 git submodule foreach git pull origin master 명령어를 실행하여 하위 submodule들을 전부 업데이트 해주는 방법