Wemos
Wemos

Reputation: 61

form post to rest api

i have been searching all the web and didnt know where to start , created an html web form and when posting the form , i need to send the value to the rest api

my html :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Create Event</title>
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<script type="text/javascript" src="view.js"></script>
</head>



<body id="main_body" >

    <img id="top" src="top.png" alt="">
    <div id="form_container">
        <h1><a>Create Event</a></h1>
        <form id="form_4068" class="appnitro"  method="POST" action="theSiteite.php">
                    <div class="form_description">
            <h2>Create Event</h2>
        </div>                      
            <ul >

            <li id="li_2" >
        <label class="description" for="Title">Title </label>
        <div>
            <input id="Title" name="Title" class="element text medium" type="text" maxlength="255" value=""/> 
        </div> 
        </li>       <li id="li_5" >
        <label class="description" for="element_5">User </label>
        <div>
        <select class="element select medium" id="element_5" name="element_5"> 
            <option value="" selected="selected"></option>
                    <option value="1" >option1</option>
                    <option value="2" >option2</option>
                    <option value="3" >option3</option>
        </select>
        </div> 
        </li>       <li id="li_1" >
        <label class="description" for="element_1">Application ID </label>
        <div>
        <select class="element select medium" id="element_1"name="element_1"> 
            <option value="" selected="selected"></option>
                <option value="1" >option1</option>
                <option value="2" >option2</option>
                <option value="3" >option3</option>

        </select>
        </div> 
        </li>
        <li id="li_3" >
        <label class="description" for="element_3">Change Log </label>
        <div>
            <textarea id="element_3" name="element_3" class="element textarea small"></textarea> 
        </div> 
        </li>
        <li id="li_4" >
        <label class="description" for="element_4">Description </label>
        <div>
            <textarea id="element_4" name="element_4" class="element textarea small"></textarea> 
        </div> 
        </li>

                    <li class="buttons">
                <input type="hidden" name="form_id" value="4068" />

                <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
        </li>
            </ul>
        </form> 
    </div>
    <img id="bottom" src="bottom.png" alt="">




    <script type="text/javascript">
    //empty
    </script>
</body>
</html>

this is the request example provided by the restApi holder

curl -X POST 'https://api.newrelic.com/v2/applications/{application_id}/deployments.json' \
     -H 'X-Api-Key:{123456789}' -i \
     -H 'Content-Type: application/json' \
     -d \
'{
  "deployment": {
    "Title": "string",
    "changelog": "string",
    "description": "string",
    "user": "string"
  }
}' 

and this is the JSON RESPONSE STRUCTURE provided :

{
  "deployment": {
    "Application id": "integer",
    "Title": "string",
    "changelog": "string",
    "description": "string",
    "user": "string",
    "timestamp": "datetime",
    "links": {
      "application": "integer"
    }
  }
}

i cant understand how to use the CURL functions neither the syntax. i tried with php but unsuccessful.

Upvotes: 1

Views: 2644

Answers (2)

shalvah
shalvah

Reputation: 895

I understand your situation because I faced a similar issue when I was new to web development.

You shouldn't post data from a form directly to an API if you're expecting a response. Forms weren't built for dealing with responses.

Here's what you should do (using simple PHP tools):

  1. Set your form action to a script you'll create, say, handler.php
  2. In handler.php, use curl to send the data to the server and obtain a response.
  3. After obtaining the response, process it however you wish (maybe save to a database), then redirect to a page of your choice by using header("Location: <address>");

Check out these links if you have trouble

All the best!

Upvotes: 2

Amresh Venugopal
Amresh Venugopal

Reputation: 9549

Looks like you need to sign up for an application id. Once you get the application id, replace the value in the link. Remember to remove the curly brackets as well.

After this paste the curl request part in your question/documentation reference into a terminal/command prompt. It should give you the response.

Also in the form tag, action can be replaced with the final link you obtain. But I recommend using an ajax call instead.

Upvotes: 1

Related Questions