'application/xml' 콘텐츠가 포함된 HTTP 요청을 파싱합니다.
코드 샘플
Go
// Package http provides a set of HTTP Cloud Functions samples.
package http
import (
"encoding/xml"
"fmt"
"html"
"io/ioutil"
"net/http"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
func init() {
functions.HTTP("ParseXML", ParseXML)
}
// ParseXML is an example of parsing a text/xml request.
func ParseXML(w http.ResponseWriter, r *http.Request) {
var d struct {
Name string
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Could not read request", http.StatusBadRequest)
}
if err := xml.Unmarshal(b, &d); err != nil {
http.Error(w, "Could not parse request", http.StatusBadRequest)
}
if d.Name == "" {
d.Name = "World"
}
fmt.Fprintf(w, "Hello, %v!", html.EscapeString(d.Name))
}
Java
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class ParseXml implements HttpFunction {
private static DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
// Parses a HTTP request in XML format
// (Responds with a 400 error if the HTTP request isn't valid XML.)
@Override
public void service(HttpRequest request, HttpResponse response)
throws IOException, ParserConfigurationException {
try {
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
var writer = new PrintWriter(response.getWriter());
// Get request body
InputStream bodyStream = new ByteArrayInputStream(
request.getInputStream().readAllBytes());
// Parse + process XML
Document doc = docBuilder.parse(bodyStream);
writer.printf("Root element: %s", doc.getDocumentElement().getNodeName());
} catch (SAXException e) {
response.setStatusCode(HttpURLConnection.HTTP_BAD_REQUEST);
return;
}
}
}
Node.js
const functions = require('@google-cloud/functions-framework');
/**
* Parses a document of type 'text/xml'
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
functions.http('parseXML', (req, res) => {
// Convert the request to a Buffer and a string
// Use whichever one is accepted by your XML parser
const data = req.rawBody;
const xmlData = data.toString();
const {parseString} = require('xml2js');
parseString(xmlData, (err, result) => {
if (err) {
console.error(err);
res.status(500).end();
return;
}
res.send(result);
});
});
Python
import json
import functions_framework
import xmltodict
@functions_framework.http
def parse_xml(request):
""" Parses a document of type 'text/xml'
Args:
request (flask.Request): The request object.
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
"""
data = xmltodict.parse(request.data)
return json.dumps(data, indent=2)
다음 단계
다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.